import { SHOW_ITEM_TYPE } from '../../config/mod_def';
import { UI_Listen, UI_Layer } from '../../config/ui_def';
import { Sdk_Platform, OPPO_AD_CONFIG } from '../../frame/constants';

cc.Class({
    extends: cc.Component,

    properties: {
        grpNode: cc.Node,
        title_lab: cc.Label,
        owner_lab: cc.Label,
        skill_lab: cc.Label,
        deatil_lab: cc.Label,
        head_node: cc.Node,
        just_node: cc.Node,
        tishi_node: cc.Node,
        setEquipBtn: cc.Node,
        lvUpBtn: cc.Node,
        lockBtn: cc.Node,
        bottomNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
        let addNum = 4 - this.grpNode.childrenCount
        let reward_prefab = he.load.get("uimod/bagItem")
        for (let i = 0; i < addNum; i++) {
            let reward_node = cc.instantiate(reward_prefab)
            this.grpNode.addChild(reward_node)
        }
    },

    updatePage(param) {
        if (param.info) {
            this.info = param.info
        }
        if (param.index >= 0) {
            this.index = param.index
        }
        if (this.pageTween) {
            this.pageTween.stop()
            this.pageTween = null
        }
        this.adSwitch(true)
        this.canAni = true
        this.updatePageInfo()

    },

    updatePageInfo() {
        let lv = this.info.equips[this.index] + 1
        let costGrp = he.db.getDataByArgs("Equip", { equip: this.index, level: lv })
        if (this.info.equips[this.index] == 0) {
            this.lock = true
            let equipInfo = he.db.getDataByArgs('Equip', { equip: this.index, level: 1 })
            this.title_lab.string = `${equipInfo.name} 未解锁`
            this.tishi_node.active = true
            this.just_node.active = false
            this.deatil_lab.node.active = false
        } else {
            this.lock = false
            this.tishi_node.active = false
            this.just_node.active = true
            this.deatil_lab.node.active = true
            let equipInfo = he.db.getDataByArgs('Equip', { equip: this.index, level: this.info.equips[this.index] })
            this.title_lab.string = `${equipInfo.name} lv.${equipInfo.level}`
            this.skill_lab.string = `${equipInfo.text}`
            if (costGrp) {
                this.deatil_lab.string = `+${equipInfo.attack}  →  +${costGrp.attack}\n+${equipInfo.attackSpeed}  →  +${costGrp.attackSpeed} `
            } else {
                this.deatil_lab.string = `+${equipInfo.attack}  \n+${equipInfo.attackSpeed}`
            }

        }
        this.head_node.getComponent("roleEquipItem").initItem(this.info, this.index)
        this.owner_lab.string = `使用者：${this.idToName(this.info.id)}`
        this.merGrpInit()
        this.checkState()
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.VIVO:
                    // this.bottomNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.bottomNode)
                    break
                case Sdk_Platform.OPPO:
                    // console.log("技能强化")
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 4,
                        position: cc.Vec2(0, -600)
                    })
                    break
            }
        } else {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    break
                case Sdk_Platform.VIVO:
                    // this.bottomNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.bottomNode)
                    break
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
            }
        }
    },
    //合成素材栏 初始化
    merGrpInit() {
        let lv = this.info.equips[this.index] + 1

        let costGrp = he.db.getDataByArgs("Equip", { equip: this.index, level: lv })
        let roleNeed = he.db.getDataByArgs("Role", { roleId: this.info.id, level: lv })
        if (costGrp == null) {
            this.canLvUp = true
            this.grpNode.active = false
        } else {
            this.grpNode.active = true
            let costNeed = `${costGrp.cost}#${roleNeed.cost}`
            // console.log(costNeed)
            this.canLvUp = gg.data.checkItemStr(costNeed, false)
            let grp = costNeed.split('#')
            // console.log("grp",grp)
            for (let j = 0; j < this.grpNode.children.length; j++) {
                this.grpNode.children[j].active = false
            }
            for (let i = 0; i < grp.length; i++) {
                // let xx = gg.data.checkItem(grp[i])
                let infoGrp = grp[i].split('_')
                this.grpNode.children[i].active = true
                let itemScript = this.grpNode.children[i].getComponent('bagItem')
                // let item_id = parseInt(infoGrp[0],SHOW_ITEM_TYPE.BY_CHECK)
                itemScript.initInfo(grp[i], SHOW_ITEM_TYPE.BY_CHECK)
                itemScript.setNum(infoGrp[1])
            }
        }
    },

    idToName(id) {
        let name = null
        switch (id) {
            case 1001:
                name = '弗特'
                break
            case 1002:
                name = '赛米'
                break
            case 1003:
                name = '露西'
                break
            case 1004:
                name = '麦克斯'
                break
            case 1005:
                name = '雷'
                break
        }
        return name
    },

    closeUI() {
        this.adSwitch(false)
        let guideList = he.local.getJson('guideList', 'user')
        if (!guideList) {
            return
        } else {
            if (!guideList[3]) {
                guideList[2] = 1
                guideList[3] = 1
                //新手引导
                gg.data.pause = true
                gg.eventManager.emit('newGuide', { index: 3 })
                he.local.setJson('guideList', guideList, 'user')
            }
        }
    },

    checkState() {
        if (!this.lock) {
            this.setEquipBtn.active = true
            this.lvUpBtn.active = true
            this.lockBtn.active = false
        } else {
            this.setEquipBtn.active = false
            this.lvUpBtn.active = false
            this.lockBtn.active = true
        }
        let lab = this.setEquipBtn.getChildByName('lab').getComponent(cc.Label)
        if (this.info.hand == this.index) {
            lab.string = '使用中'
            this.setEquipBtn.color = (cc.Color.GRAY)
            this.setEquipBtn.getComponent(cc.Button).interactable = false
        } else {
            lab.string = '装备'
            this.setEquipBtn.color = (cc.Color.WHITE)
            this.setEquipBtn.getComponent(cc.Button).interactable = true
        }
    },

    closeServer() {
        this.uiBase.closePage()
    },

    setEquipServer() {
        gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_SETEQUIP })
        let lab = this.setEquipBtn.getChildByName('lab').getComponent(cc.Label)
        lab.string = '使用中'
        this.setEquipBtn.color = (cc.Color.GRAY)
        this.setEquipBtn.getComponent(cc.Button).interactable = false
        // this.closeServer()
    },

    lvUpBtnServer() {
        if (!this.canAni) return
        this.canAni = false

        if (this.canLvUp) {
            let lv = this.info.equips[this.index] + 1
            let equipInfo = he.db.getDataByArgs("Equip", { equip: this.index, level: lv })
            let roleNeed = he.db.getDataByArgs("Role", { roleId: this.info.id, level: lv })
            if (equipInfo == null) {
                gg.uiManager.addTip('已升至最高等级')
            } else {
                let grp = `${equipInfo.cost}#${roleNeed.cost}`
                gg.data.checkItemStr(grp, true)
                let equipLv = this.info.equips[this.index]
                if (equipLv < 10) {
                    equipLv += 1
                }

                this.info.equips[this.index] = equipLv
                this.team = he.local.getJson('team', 'user')
                // let team = roleTeam
                this.pageIndex = null
                for (let j = 0; j < this.team.length; j++) {
                    if (this.team[j].id == this.info.id) {
                        this.pageIndex = j
                        this.team[j] = this.info
                        break
                    }
                }
                he.local.setJson('team', this.team, 'user')
                let taskGrp = he.local.getJson('task', 'user')
                if (taskGrp && taskGrp.length > 0) {
                    for (let i = 0; i < taskGrp.length; i++) {
                        if (taskGrp[i].type == 9004 && (taskGrp[i].num < taskGrp[i].need)) {
                            taskGrp[i].num += 1
                            break
                        }
                    }
                    he.local.setJson('task', taskGrp, 'user')
                }
            }
            this.pageTween = cc.tween(this.node)
                .to(0.2, { scale: 1.1 })
                .to(0.1, { scale: window.zoom_scale })
                .call(() => {
                    this.canAni = true
                })
                .start()

            gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_LVUPBTN, index: this.pageIndex })
            this.updatePageInfo()

            // this.closeServer()
            // this.showPanal()
            // this.initPageView(index)
        } else {
            let guide = he.local.getJson('guideList', 'user')
            if (!guide[3]) return
            this.canAni = true
            let lv = this.info.equips[this.index] + 1
            let costGrp = he.db.getDataByArgs("Equip", { equip: this.index, level: lv })
            let roleNeed = he.db.getDataByArgs("Role", { roleId: this.info.id, level: lv })
            let costNeed = `${costGrp.cost}#${roleNeed.cost}`
            if (gg.data.checkIcon(costNeed))
                gg.uiManager.addTip('素材不足')
        }
    },
});
